#!/usr/bin/env lua

--------------------------------------------------------------------------------
--	This file is part of the Lua HUD for TASing Sega Genesis Sonic games.
--	
--	This program is free software: you can redistribute it and/or modify
--	it under the terms of the GNU Lesser General Public License as 
--	published by the Free Software Foundation, either version 3 of the 
--	License, or (at your option) any later version.
--	
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--	
--	You should have received a copy of the GNU Lesser General Public License
--	along with this program.  If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------

-- Edit these directories (after the 'or') to match your setup.
romdir  = os.getenv("ROMDIR")  or homedir .. "/.wine/drive_c/games/gens/ROMS/GenRen/"
hackdir = os.getenv("HACKDIR") or homedir .. "/.wine/drive_c/games/gens/ROMS/"
cddir   = os.getenv("CDDIR")   or homedir .. "/.wine/drive_c/games/gens/ROMS/"

outfile = io.open("sonic/common/hud-codes.lua", "wb")
outfile:write("-------------------------------------------------------------------------------\n")
outfile:write("--	DO NOT EDIT THIS FILE, IT IS AUTOGENERATED!\n")
outfile:write("-------------------------------------------------------------------------------\n\n")
outfile:write("huds = {\n")

for game = 1,2 do
	if game == 1 then
		files = {
			[01]={"s2"    , romdir .. "Sonic the Hedgehog 2 (W) [!].bin"},
			[02]={"s2knux", romdir .. "Sonic and Knuckles & Sonic 2 (W) [!].bin"},
			[03]={"s2amy" , hackdir .. "Amy_In_Sonic_2_Rev_1.5.bin"},
			[04]={"s2rob" , hackdir .. "Robotnik's Revenge v1.bin"},
			[05]={"s1and2", hackdir .. "Sonic 1 and 2.bin"},
			[06]={"s2boom", hackdir .. "SBOOM.BIN"},
		}
		hudcode = {0x36, 0x3c, 0x00, 0x90, 0x34, 0x3c, 0x01, 0x08, 0x43, 0xf9, 0x00}
	elseif game == 2 then
		files = {
			[01]={"sk"    , romdir .. "Sonic and Knuckles & Sonic 3 (W) [!].bin"},
			[02]={"s3"    , romdir .. "Sonic the Hedgehog 3 (U) [!].bin"},
			[03]={"s3kamy", hackdir .. "Sonic_3_And_Amy_Rev_1.3.bin"},
			}
		hudcode = {0x06, 0x40, 0x00, 0x8f, 0x32, 0x3c, 0x01, 0x08, 0x3a, 0x3c, 0x86, 0xca, 0x43, 0xfa, 0x00, 0x16, 0xd2, 0xf1, 0x40, 0x00, 0x38, 0x19, 0x53, 0x44, 0x6b}
	end

	for n = 1, #files do
		fid, filename = files[n][1], files[n][2]
		file = io.open(filename, "rb")
		if file == nil then
			print("Warning: File '" .. filename .. "' was not found, and is being ignored.")
			print("You will not be able to turn off the HUD for this game/hack unless you rerun this script with the missing file present.\n")
		else
			bytes = file:read("*a")

			start = 1
			if fid == "s2knux" then
				start = 3 * 1024 * 1024
			end
			for i = start, #bytes do
				if hudcode[1] < 0 or bytes:byte(i) == hudcode[1] then
					found = true
					for j = 2, #hudcode do
						if hudcode[j] >= 0 and bytes:byte(i + j - 1) ~= hudcode[j] then
							found = false
							break
						end
					end
					if found then
						outfile:write(string.format("\t%-7s = 0x%06x,\n", fid, i + 7))
						break
					end
				end
			end
		end
	end
end

outfile:write("}\n\n")


